{extend name="$lake_admin_layout"/}

{block name="main"}
<div class="layui-card">
    <div class="layui-card-header layui-bg-cyan">模型字段</div>
    <div class="layui-card-body">
        <table class="layui-hide" id="table" lay-filter="table"></table>
    </div>
</div>

<script type="text/html" id="showTypeTpl">
    {{#  if (d.show_type == 1) { }}
        <span class="layui-badge layui-bg-green">全部显示</span>
    {{#  } }}

    {{#  if (d.show_type == 2) { }}
        <span class="layui-badge layui-bg-blue">添加显示</span>
    {{#  } }}

    {{#  if (d.show_type == 3) { }}
        <span class="layui-badge layui-bg-cyan">编辑显示</span>
    {{#  } }}

    {{#  if (d.show_type == 0) { }}
        <span class="layui-badge">全不显示</span>
    {{#  } }}
</script>

<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-sm layui-btn-normal layui-iframe" href="{:url('add',['modelid'=>$modelid])}" title="添加字段">添加字段</a>
    </div>
</script>

<script type="text/html" id="isMustTpl">
    {{#  if(d.is_must == 1){ }}
        <span class='layui-badge-dot layui-bg-green'></span>
        <span class='text-green'>是</span>
    {{#  } else { }}
        <span class='layui-badge-dot layui-bg-orange'></span>
        <span class='text-orange'>否</span>
    {{#  } }}
</script>

<script type="text/html" id="switchTpl">
    <input type="checkbox" name="status" data-href="{:url('state')}?id={{d.id}}" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="switchStatus" {{ d.status==1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="barTool">
    <a href='{:url("edit")}?id={{ d.id }}' class="layui-btn layui-btn-xs layui-iframe" title="编辑字段【{{ d.name }}】">编辑</a>
    <a href='{:url("delete")}?id={{ d.id }}' class="layui-btn layui-btn-danger layui-btn-xs layui-tr-del">删除</a>
</script>
<script type="text/html" id="createTimeTpl">
    {{ layui.util.toDateString(d.add_time * 1000) }}
</script>
{/block}

{block name="script"}
<script>
layui.use(['table', 'notice'], function() {
    var table = layui.table,
        $ = layui.$,
        notice = layui.notice
        form = layui.form;
        
    table.render({
        elem: '#table',
        toolbar: '#toolbar',
        url: '{:url("index", ["modelid" => $modelid])}',
        cols: [
            [
                { field: 'id', width: 60, title: '#', align: "center" },
                { field: 'name', title: '字段名称' },
                { field: 'title', title: '标题' },
                { field: 'type', width:120, title: '字段类型' },
                { field: 'add_time',width:180,title: '创建时间', templet: '#createTimeTpl' },
                { field: 'is_must', width: 80, align:"center", title: '必填', templet: '#isMustTpl', unresize: true },
                { field: 'sort', width: 60, align:"center", title: '排序', edit: 'text' },
                { field: 'status', width: 100, align:"center", title: '状态', templet: '#switchTpl', unresize: true },
                { fixed: 'right', width: 120, title: '操作', templet: '#barTool' }
            ]
        ],
        page: 20,
    });

    // 监听单元格编辑
    table.on('edit(table)', function(obj) {
        var value = obj.value,
            data = obj.data;
        $.post('{:url("sort")}', { 'id': data.id, 'value': value }, function(data) {
            if (data.code == 1) {
                notice.success(data.msg);
            } else {
                notice.warning(data.msg);
            }

        })
    });
});
</script>
{/block}